package com.cbw.blog.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cbw.blog.entity.Article;
import com.cbw.blog.entity.Statistic;
import com.cbw.blog.mapper.ArticleMapper;
import com.cbw.blog.service.IArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cbw.blog.service.IStatisticService;

import com.vdurmont.emoji.EmojiParser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2022-09-05
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {
@Autowired
IStatisticService statisticService;


    @Autowired
    StringRedisTemplate redisTemplate;


public List<Article> getHeatArticles(){

    //查询热度前10的评论
    QueryWrapper<Statistic> wrapper = new QueryWrapper<Statistic>()
            .ne("hits",0)
            .orderByDesc("hits")
            .orderByDesc("comments_num");
    Page<Statistic> statisticPage = new Page<>(1, 10);
    statisticService.page(statisticPage,wrapper);

    //查询热度最高的详细信息
if (statisticPage.getRecords()!=null&&statisticPage.getRecords().size()>0){
    List<Article> collect = statisticPage.getRecords().stream().map(item -> {
        Article byId = this.getById(item.getArticleId());
        byId.setHits(item.getHits());
        return byId;
    }).collect(Collectors.toList());

    return collect;
}

return null;
}

    @Override
    public Article selectArticleWithId(Integer id) {
        Article article=null;
        String s = redisTemplate.opsForValue().get("article_" + id);
        if (!StringUtils.isEmpty(s)){
            article = JSON.parseObject(s, Article.class);
        }else {
            article = this.getById(id);
            String articleJSON = JSON.toJSONString(article);
            redisTemplate.opsForValue().set("article_" + id,articleJSON,60, TimeUnit.MINUTES);
        }
        return article;

    }

    @Override
    public void pulish(Article article) {
        article.setContent(EmojiParser.parseToAliases(article.getContent()));
        article.setCreated(new Date());
        article.setHits(0);
        article.setCommentsNum(0);
        baseMapper.insert(article);
        Statistic statistic = new Statistic();
        statistic.setArticleId(article.getId());
        statistic.setHits(0);
        statistic.setCommentsNum(0);
        statisticService.save(statistic);
    }

    @Override
    public void updateArticleWithId(Article article) {
        article.setModified(new Date());
       baseMapper.updateArticleWithId(article);
       redisTemplate.delete("article_"+article.getId());
    }


}
